<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path'), EXT);

use app\admin\Controller;
use think\Loader;

class Withdraw extends Controller{
    use \app\admin\traits\controller\Controller {
        \app\admin\traits\controller\Controller::index as indexOld;
    }
    private function getList($listRows=20,$asc=false) {
        $model = Loader::model('Withdraw');
        // 列表过滤器，生成查询Map对象
        $where = "1=1";
        // 过滤器
        if ($this->request->param('sn')) {
            $where .= " AND p.sn LIKE '%".$this->request->param('sn')."%'";
        }
        if ($this->request->param('pay_sn')) {
            $where .= " AND p.pay_sn LIKE '%".$this->request->param('pay_sn')."%'";
        }
        if ( $this->request->param('status') || is_numeric($this->request->param('status')) ) {
            $where .= " AND p.status='".$this->request->param('status')."'";
        }
        if ($this->request->param('nickname')) {
            $where .= " AND u.nickname LIKE '%".$this->request->param('nickname')."%'";
        }
        if ($this->request->param('mobile')) {
            $where .= " AND u.mobile LIKE '%".$this->request->param('mobile')."%'";
        }

        // 排序字段 默认为主键名
        $order = $this->request->param('_order') ? : ($model->getPk());
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $sort = null !== $this->request->param('_sort')
            ? ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc')
            : ($asc ? 'asc' : 'desc');
        $order_by = $order ? "{$order} {$sort}" : '';
        // 查询字段
        $field = 'p.id,p.sn,p.status,p.money,p.pay_sn,p.pay_time,p.pay_type,p.rate,p.rate_money,p.create_time,u.nickname,u.mobile,u.headimg,u.openid';
        return $model->getList($field,$where,$listRows,$this->request->get(),$order_by);
    }
    /**
     * 列表
     */
    public function index() {
        $listRows = 20;
        $list = $this->getList($listRows,0);

        // 模板赋值显示
        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);

        return $this->view->fetch();
    }
    /*处理支付*/
    public function resume(){
        $id = (int)$this->request->param('id');
        if( 0 === $id ) {
            return ajax_return_adv_error('缺少必要参赛');
        }
        $model = Loader::model('Withdraw');
        $data = $model->getPayParameter($id);
        if( $data ) {
            vendor('wxpay.transfers');
            $pay = new \transfers();
            if( 1 === (int)$data['status'] ) {
                return ajax_return_adv_error('已经付款,不需要重复操作！');
            }
            //计算汇率
            if( $data['money'] > $data['min'] ) {
                //扣除汇率
                $rate_money = round($data['money']*$data['rate'],2);
                $rate_money > $data['rate_max'] && $rate_money = $data['rate_max'];
                $money = round($data['money'] - $data['money']*$data['rate'],2);
                if( 0 >= $money ) {
                    return ajax_return_adv_error('提现金额不能错误【'.$money.'】');
                }
            } else {
                $rate_money = 0;
                $money = $data['money'];
            }
            $pay->setConfig([
                'APPID'=>$data['appid'],
                'MCHID'=>$data['mchid'],
                'KEY'=>$data['key'],
                'APPSECRET'=>$data['appsecret'],
            ]);

            $request = $pay->send([
                'partner_trade_no'=>$data['sn'],
                'openid'=>$data['openid'],
                'check_name'=>'NO_CHECK',
                're_user_name'=>$data['realname'],
                'amount'=>$money*100,
                'desc'=>'申请提现',
                'spbill_create_ip'=>$this->request->ip()
            ]);
            if( 'SUCCESS' === $request['return_code'] ) {
                if( 'SUCCESS' === $request['result_code'] ) {
                    $model->setPaySuccess(
                        $data['id'],
                        [
                            'pay_sn'=>$request['payment_no'],
                            'rate'=>$data['rate'],
                            'rate_money'=>$rate_money,
                            'pay_type'=>'微信转账',
                        ],
                        $money);

                    return ajax_return_adv('提现成功','current');
                } else {
                    return ajax_return_adv_error($request['return_msg'].'=>'.$request['err_code_des']);
                }
            } else {
                return ajax_return_adv_error('请求失败！');
            }
        }
    }

    public function user_ext(){
        $listRows = 50;
        $model = Loader::model('User');
        // 列表过滤器，生成查询Map对象
        $where = "1=1";
        // 过滤器
        if ($this->request->param('nickname')) {
            $where .= " AND nickname LIKE '%".$this->request->param('nickname')."%'";
        }
        if ($this->request->param('mobile')) {
            $where .= " AND mobile LIKE '%".$this->request->param('mobile')."%'";
        }
        $min_money = $this->request->param('min_money')?:-9999;
        $max_money = $this->request->param('max_money')?:9999999999;
        $where .= " AND money1 BETWEEN $min_money AND $max_money";
        // 排序字段 默认为主键名
        $order = $this->request->param('_order') ? : 'money1';
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $sort = null !== $this->request->param('_sort')
            ? ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc')  : 'desc';
        $order_by = $order ? "{$order} {$sort}" : 'money1 desc';
        // 查询字段
        $field = 'a.*,b.nickname,b.mobile';
        $list = $model->getUserExt($field,$where,$listRows,$this->request->get(),$order_by);
        // 模板赋值显示
        $this->view->assign('money', $model->getUserExtMoney($where)?:0);

        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);
        return $this->view->fetch();
    }
}
